package com.easyattendance.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.easyattendance.model.Holidays;
import com.easyattendance.services.AddHolidayListService;
import com.easyattendance.services.AddSectionService;

/**
 * Servlet implementation class AddHolidayListServlet
 */
@WebServlet(name="AddHolidayListServlet",urlPatterns="/AddHolidayListServlet")
public class AddHolidayListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
    	response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		boolean dispatcherFlag = false;
		boolean result = false;
		AddSectionService sectionService = new AddSectionService();
		AddHolidayListService holidayService = new AddHolidayListService();
		
		String sectionEmailId = request.getParameter("section_selected");
		String holiday = null;
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date fromDate = null;
		Date toDate = null;
				
		int rowCount = Integer.parseInt(request.getParameter("row_count"));
		System.out.println("Row count: "+ rowCount);
		
		for(int i=1; i<=rowCount; i++) {
			holiday = request.getParameter("holiday"+i);
			String start = request.getParameter("from"+i);
			String end =  request.getParameter("to"+i);
			
			if(start == null || end == null || holiday == null || sectionEmailId == null) {
				request.setAttribute("null_values", "Values cannot be null");
	        	dispatcherFlag = true;
				break;
			}
			else {
				try {
					fromDate = format.parse(start);
					toDate = format.parse(end);
				} catch (ParseException e) {
					e.printStackTrace();
				}
				
				Holidays newHoliday = new Holidays(holiday, fromDate, toDate, sectionService.getSectionBySectionEmail(sectionEmailId).getSection_id(), sectionService.getSectionBySectionEmail(sectionEmailId));
				
				boolean res = holidayService.ifAlreadyExists(newHoliday);
				
				if(!res){
					//if(result && i!= 1){
						result = holidayService.addHolidays(newHoliday);
					//}
					
				}
				else {
					request.setAttribute("holiday_exists", holiday+" holiday already exists");
		        	dispatcherFlag = true;
				}
			}
		}
		
		if(result) {
			request.setAttribute("holidays_added", "Holidays added successfully");
        	dispatcherFlag = true;
		}
		
		if(dispatcherFlag) {
			RequestDispatcher rd = request.getRequestDispatcher("views/section/setHolidays.jsp");
	    	rd.forward(request, response);
		}
		
		
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

}
